package com.bfxy.rocketmq.common;

import com.bfxy.rocketmq.model.Student;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

@Slf4j
public class StreamDemo {

    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(Student.builder().id("1").name("張三").score("82").build());
        students.add(Student.builder().id("2").name("李四").score("68").build());
        students.add(Student.builder().id("3").name("王五").score("78").build());
//        Stream<Student> student = students.stream().filter(new Predicate<Student>() {
//            public boolean test(Student student1) {
//                return "張三".equals(student1.getName());//只保留男性
//            }
//        });
//        Stream<Student> student = students.stream().filter(
//                student1 -> "張三".equals(student1.getName())
//        );
//        students = student.collect(Collectors.toList());//将Stream转化为List
        students.removeIf(student -> "張三".equals(student.getName()));
//        students.removeIf(student -> "李四".equals(student.getName()));
        System.out.println(students.toString());//查看结果
        //函数式接口
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        eval(list,n->true);
        Optional<Integer> a = Optional.ofNullable(null);
        Optional<String>  b = Optional.ofNullable("123");
        test(a,b);
    }

    public static void eval(List<Integer> list, Predicate<Integer> predicate) {
        for(Integer n: list) {
            if(predicate.test(n)) {
                System.out.println(String.format("n is %s",n));
            }
        }
    }
    public static void test(Optional<Integer> a, Optional<String> b) {
        if(a.isPresent()){
            System.out.println("存在");
        }else {
            System.out.println("不存在");
        }
        log.info("a is {},b is {}",a.isPresent(),b.isPresent());
    }
}
